package simplest;

import com.rabbitmq.client.*;
import util.ConnectionRabbitMQ;

import java.io.IOException;

/**
 * 消息接收者
 * ACK确认机制
 */
public class RecerByACK {
    public static void main(String[] args) throws Exception{
        // 1.获得连接
        Connection connection = ConnectionRabbitMQ.getConnection();
        // 2.在连接中创建通道
        final Channel channel = connection.createChannel();
        // 3.从信道中获得信息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                String s = new String(body);
                System.out.println("接收到的消息是:"+s);
                // 手动确认(收件人信息,是否同时确认多个消息)
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        // 4.监听队列  false:手动消息确认机制
        channel.basicConsume("queue1",true,consumer);

    }
}
